package com.snmp.smn.monitors;

import java.net.URL;

import com.snmp.smn.beans.Device;
import com.snmp.smn.utils.EmailSender;
import com.snmp.smn.utils.Utilities;

public class HTTPMonitor extends DeviceMonitor {
	
	public HTTPMonitor(Device device) {
		this.device = device;
	}

	public void checkDevice() throws Exception {
		Utilities.log("Checking device " + device);
		
		try {
			URL url = new URL("http://" + device.getDeviceIP() + ":" +
								device.getDevicePort());
			url.openConnection().getContent();
			
			Utilities.log("Successfully checked device " + device);
			
			updateSuccessfulCheck();
		} catch (Exception e) {
			Utilities.log("Unabled to check device " + device);
			
			updateFailedCheck();
			
			EmailSender.sendDeviceFailed(device.getDeviceId(), device.getDeviceName(), 
											device.getDeviceIP(), device.getDevicePort(),
												e.getMessage());
			
			restartDevice();
		}
	}
}
